home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / bin / zgrep < prev    next >
Encoding:
Text File  |  2008-10-15  |  4.8 KB  |  189 lines

  1. #!/bin/bash
  2.  
  3. # zgrep -- a wrapper around a grep program that decompresses files as needed
  4. # Adapted from a version sent by Charles Levert <charles@comm.polymtl.ca>
  5.  
  6. # Copyright (C) 1998, 2001, 2002, 2006, 2007 Free Software Foundation
  7. # Copyright (C) 1993 Jean-loup Gailly
  8.  
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13.  
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. # GNU General Public License for more details.
  18.  
  19. # You should have received a copy of the GNU General Public License along
  20. # with this program; if not, write to the Free Software Foundation, Inc.,
  21. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22.  
  23. PATH=${GZIP_BINDIR-'/bin'}:$PATH
  24. grep='${GREP-grep}'
  25.  
  26. version='z$grep (gzip) 1.3.12
  27. Copyright (C) 2007 Free Software Foundation, Inc.
  28. This is free software.  You may redistribute copies of it under the terms of
  29. the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
  30. There is NO WARRANTY, to the extent permitted by law.
  31.  
  32. Written by Jean-loup Gailly.'
  33.  
  34. usage="Usage: $0 [OPTION]... [-e] PATTERN [FILE]...
  35. Look for instances of PATTERN in the input FILEs, using their
  36. uncompressed contents if they are compressed.
  37.  
  38. OPTIONs are the same as for 'grep'.
  39.  
  40. Report bugs to <bug-gzip@gnu.org>."
  41.  
  42. # sed script to escape all ' for the shell, and then (to handle trailing
  43. # newlines correctly) turn trailing X on last line into '.
  44. escape='
  45.   s/'\''/'\''\\'\'''\''/g
  46.   $s/X$/'\''/
  47. '
  48. operands=
  49. have_pat=0
  50. files_with_matches=0
  51. files_without_matches=0
  52. no_filename=0
  53. with_filename=0
  54.  
  55. while test $# -ne 0; do
  56.   option=$1
  57.   shift
  58.   optarg=
  59.  
  60.   case $option in
  61.   (-[0123456789abcdhHiIKLlnoqrRsTuUvVwxyzZ]?*)
  62.     arg2=-\'$(expr "X${option}X" : 'X-.[0-9]*\(.*\)' | sed "$escape")
  63.     eval "set -- $arg2 "'${1+"$@"}'
  64.     option=$(expr "X$option" : 'X\(-.[0-9]*\)');;
  65.   (--binary-*=* | --[lm]a*=* | --reg*=*)
  66.     ;;
  67.   (-[ABCDefm] | --binary-* | --file | --[lm]a* | --reg*)
  68.     case ${1?"$option option requires an argument"} in
  69.     (*\'*)
  70.       optarg=" '"$(printf '%sX\n' "$1" | sed "$escape");;
  71.     (*)
  72.       optarg=" '$1'";;
  73.     esac
  74.     shift;;
  75.   (--)
  76.     break;;
  77.   (-?*)
  78.     ;;
  79.   (*)
  80.     case $option in
  81.     (*\'*)
  82.       operands="$operands '"$(printf '%sX\n' "$option" | sed "$escape");;
  83.     (*)
  84.       operands="$operands '$option'";;
  85.     esac
  86.     ${POSIXLY_CORRECT+break}
  87.     continue;;
  88.   esac
  89.  
  90.   case $option in
  91.   (-[drRzZ] | --di* | --exc* | --inc* | --rec* | --nu*)
  92.     printf >&2 '%s: %s: option not supported\n' "$0" "$option"
  93.     exit 2;;
  94.   (-[ef]* | --file | --file=* | --reg*)
  95.     have_pat=1;;
  96.   (--h | --he | --hel | --help)
  97.     echo "$usage" || exit 2
  98.     exit;;
  99.   (-H | --wi | --wit | --with | --with- | --with-f | --with-fi \
  100.   | --with-fil | --with-file | --with-filen | --with-filena | --with-filenam \
  101.   | --with-filename)
  102.     with_filename=1
  103.     continue;;
  104.   (-l | --files-with-*)
  105.     files_with_matches=1;;
  106.   (-L | --files-witho*)
  107.     files_without_matches=1;;
  108.   (-h | --no-f*)
  109.     no_filename=1;;
  110.   (-V | --v | --ve | --ver | --vers | --versi | --versio | --version)
  111.     echo "$version" || exit 2
  112.     exit;;
  113.   esac
  114.  
  115.   case $option in
  116.   (*\'?*)
  117.     option=\'$(expr "X${option}X" : 'X\(.*\)' | sed "$escape");;
  118.   (*)
  119.     option="'$option'";;
  120.   esac
  121.  
  122.   grep="$grep $option$optarg"
  123. done
  124.  
  125. eval "set -- $operands "'${1+"$@"}'
  126.  
  127. if test $have_pat -eq 0; then
  128.   case ${1?"missing pattern; try \`$0 --help' for help"} in
  129.   (*\'*)
  130.     grep="$grep -- '"$(printf '%sX\n' "$1" | sed "$escape");;
  131.   (*)
  132.     grep="$grep -- '$1'";;
  133.   esac
  134.   shift
  135. fi
  136.  
  137. if test $# -eq 0; then
  138.   set -- -
  139. fi
  140.  
  141. exec 3>&1
  142. res=0
  143.  
  144. for i
  145. do
  146.   # Fail if gzip or grep (or sed) fails.
  147.   gzip_status=$(
  148.     exec 5>&1
  149.     (gzip -cdfq -- "$i" 5>&-; echo $? >&5) 3>&- |
  150.     if test $files_with_matches -eq 1; then
  151.       eval "$grep" >/dev/null && { printf '%s\n' "$i" || exit 2; }
  152.     elif test $files_without_matches -eq 1; then
  153.       eval "$grep" >/dev/null || {
  154.     r=$?
  155.     if test $r -eq 1; then
  156.       printf '%s\n' "$i" || r=2
  157.     fi
  158.     exit $r
  159.       }
  160.     elif test $with_filename -eq 0 &&
  161.      { test $# -eq 1 || test $no_filename -eq 1; }; then
  162.       eval "$grep"
  163.     else
  164.       case $i in
  165.       (*'
  166. '* | *'&'* | *'\'* | *'|'*)
  167.         i=$(printf '%s\n' "$i" |
  168.         sed '
  169.           $!N
  170.           $s/[&\|]/\\&/g
  171.           $s/\n/\\n/g
  172.         ');;
  173.       esac
  174.       sed_script="s|^|$i:|"
  175.  
  176.       # Fail if grep or sed fails.
  177.       r=$(
  178.     exec 4>&1
  179.     (eval "$grep" 4>&-; echo $? >&4) 3>&- | sed "$sed_script" >&3 4>&-
  180.       ) || r=2
  181.       exit $r
  182.     fi >&3 5>&-
  183.   )
  184.   r=$?
  185.   test "$gzip_status" -eq 0 || test "$gzip_status" -eq 2 || r=2
  186.   test $res -lt $r && res=$r
  187. done
  188. exit $res
  189.